#include<stdio.h>
#include<stdlib.h>

#define MAXN 30
typedef int ElementType;

typedef  struct  TreeNode *BinTree;
typedef BinTree ElementType;/*二叉树类型*/
/*树结点定义*/
struct  TreeNode {
	int  data;/*结点数据*/
	BinTree  left;/*指向左子树*/
	BinTree  right;/*指向右子树*/
};

BinTree BuildTree(int Inorder[], int Postorder[], int N);/*根据中序和后序数组中的N个结点建树*/
void PreorderTraversal(BinTree);/*先序遍历*/

int main()
{
	BinTree T;
	int Inorder[MAXN], Postorder[MAXN], N, i;
	scanf_s("%d", &N);
	for (i = 0; i < N; i++)
		scanf_s("%d", &Postorder[i]);
	for (i = 0; i < N; i++)
		scanf_s("%d", &Inorder[i]);
	T = BuildTree(Inorder, Postorder, N);
	printf("Preorder: ");
	PreorderTraversal(T);
	printf("\n");
	return 0;
}

BinTree BuildTree(int Inorder[], int Postorder[], int N)
{
	BinTree T;
	int p;
	if (!N)/*递归终止条件：空树*/
		return NULL;
	T = (BinTree)malloc(sizeof(struct TreeNode));
	T->data = Postorder[N - 1];/*根结点是后序最后一个*/
	T->left = T->right = NULL;

	for (p = 0; p < N; p++)/*在中序里找根结点*/
	{
		if (Inorder[p] == Postorder[N - 1])
			break;
	}
	T->left = BuildTree(Inorder, Postorder, p);
	T->right = BuildTree(Inorder + p + 1, Postorder + p, N - p - 1);
	return T;
}
void PreorderTraversal(BinTree T)
{
	if(T)
	{
		printf("%d", T->data);
		PreorderTraversal(T->left);
		PreorderTraversal(T->right);
	}
}

